package com.undsf.kanmu.kcsapi.controllers

import com.fasterxml.jackson.core.type.TypeReference
import com.fasterxml.jackson.databind.ObjectMapper
import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper
import com.fasterxml.jackson.module.kotlin.readValue
import com.undsf.kanmu.kcsapi.messages.BaseMessage
import com.undsf.kanmu.kcsapi.messages.Start2
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RestController
import redis.clients.jedis.Jedis
import java.io.File

/**
 * Created by Arathi on 2017/8/24.
 */
@RestController
class OtherController {
    val objMapper: ObjectMapper = jacksonObjectMapper()
    val redis: Jedis = Jedis("localhost", 6379)

    @RequestMapping("/start2")
    fun start2(): BaseMessage<Start2> {
        var content: String? = redis.get("start2")
        if (content.isNullOrEmpty()) {
            var filename = "D:\\Workspace\\undsf\\kanmulation-project\\messages\\start2.json"
            val file = File(filename)
            content = file.readText()
            // content = text.substring(text.indexOf("=")+1)
            redis.set("start2", content)
        }
        // var type: TypeReference<BaseMessage<Start2>> = TypeReference<BaseMessage<Start2>>() {}
        var message: BaseMessage<Start2> = objMapper.readValue(content ?: "")
        return message
    }
}
